{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a3dd662e-e3f1-43c3-bb0b-e6df147ca416",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "data = np.asmatrix([[1,2],[3,4],[5,6]])\n",
    "print(data)\n",
    "# 创建第二个矩阵\n",
    "data2 = np.asmatrix([1,2])\n",
    "print(data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bceaad73-b434-4d45-90b1-dce7e41792d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4]\n",
      " [4 6]\n",
      " [6 8]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的加法运算\n",
    "print(data+data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3d795036-713d-47dd-b99d-9b4ddfa4d039",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0]\n",
      " [2 2]\n",
      " [4 4]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的减法运算\n",
    "print(data-data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "55ca8a3c-a289-48c8-8a58-6caf12393651",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1.]\n",
      " [3. 2.]\n",
      " [5. 3.]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的除法运算\n",
    "print(data/data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4477a8c7-387b-4139-b1e5-38616bc43c18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 7 10]\n",
      " [15 22]\n",
      " [23 34]]\n"
     ]
    }
   ],
   "source": [
    "# 乘法运算 乘法有个规则就是左边矩阵的列和右边矩阵的行数一致,,,行*列\n",
    "data3=np.asmatrix([[1,2],[3,4]])\n",
    "print(data*data3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e6888b27-7f12-41f5-9f55-a5100721b063",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 2]\n",
      "数组的乘积： [[ 1  4]\n",
      " [ 3  8]\n",
      " [ 5 12]]\n",
      "数组的点积： [ 5 11 17]\n"
     ]
    }
   ],
   "source": [
    "# 数组的相乘于数组的点积\n",
    "n1=np.array([[1,2],[3,4],[5,6]])\n",
    "n2=np.array([1,2])\n",
    "print(n1)\n",
    "print(n2)\n",
    "print('数组的乘积：', n1*n2)\n",
    "print('数组的点积：',np.dot(n1,n2))  # 相乘完再求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3797a26d-804f-44af-b3bc-cefec212dc92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  3  3]\n",
      " [ 4  5  6]\n",
      " [ 7 12  9]]\n",
      "[[ 2  6  6]\n",
      " [ 8 10 12]\n",
      " [14 24 18]]\n",
      "矩阵相乘的结果： [[ 68 108  96]\n",
      " [132 218 192]\n",
      " [236 378 348]]\n",
      "矩阵对应元素相乘： [[  2  18  18]\n",
      " [ 32  50  72]\n",
      " [ 98 288 162]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的点积与矩阵元素之间的相乘运算\n",
    "n1=np.asmatrix('1 3 3;4 5 6;7 12 9')\n",
    "n2=np.asmatrix('2 6 6;8 10 12;14 24 18')\n",
    "print(n1)\n",
    "print(n2)\n",
    "print('矩阵相乘的结果：', n1 * n2) # 点积\n",
    "print('矩阵对应元素相乘：',np.multiply(n1, n2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "da6984c1-6378-4921-b18a-cbf0f5e90ae0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4  7]\n",
      " [ 3  5 12]\n",
      " [ 3  6  9]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的转置\n",
    "print(n1.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "475d77d5-e4c8-4da4-8279-43717f59ee9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.9         0.3         0.1       ]\n",
      " [ 0.2        -0.4         0.2       ]\n",
      " [ 0.43333333  0.3        -0.23333333]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的求逆\n",
    "print(n1.I)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
